<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes//lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_warehouse.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');

if ($_REQUEST['act'] == 'change_warehouse')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$warehouse_id=intval($_REQUEST['warehouse_id']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	
	$admin_id=$warehouse_info['admin_id'];
	
	//修改仓库为新选的仓库
	if(lock_table($warehousing_id,'warehousing','edit'))
	{
		//修改仓库
		$sql="update ".$ecs->table('erp_warehousing')." set warehouse_id='".$warehouse_id."' where warehousing_id='".$warehousing_id."'";
		$db->query($sql);		
		
		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}


if ($_REQUEST['act'] == 'change_warehousing_from')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	//检查参数
	if(empty($_REQUEST['warehousing_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$warehousing_from=trim($_REQUEST['warehousing_from']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}

	//修改仓库为新选的仓库
	if(lock_table($warehousing_id,'warehousing','edit'))
	{
		$sql="update ".$ecs->table('erp_warehousing')." set warehousing_from='".$warehousing_from."' where warehousing_id='".$warehousing_id."'";
		$db->query($sql);		
		
		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}

elseif ($_REQUEST['act'] == 'change_warehousing_style')
{
	include('../includes/cls_json.php');
	$json  = new JSON;

	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$warehousing_style_id=intval($_REQUEST['warehousing_style_id']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库类型是否存在，是否已被删除
	$warehousing_style_info=is_warehousing_style_exists($warehousing_style_id);
	if(!$warehousing_style_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehousing_style_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($warehousing_id,'warehousing','edit'))
	{
	
		//修改入仓类型
		$sql="update ".$ecs->table('erp_warehousing')." set warehousing_style_id ='".$warehousing_style_id."' where warehousing_id='".$warehousing_id."'";
		$db->query($sql);
		
		//如果是不是关联订单入库
		if($warehousing_style_id!=1)
		{			
			//清空订单号
			$sql="update ".$ecs->table('erp_warehousing')." set order_id='0' where warehousing_id='".$warehousing_id."'";
			$db->query($sql);
			//删除所有入库订单项
			$sql="delete from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."'";
			$db->query($sql);
		}
		

		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'change_order')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$order_id=intval($_REQUEST['order_id']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断订单是否存在，是否已被删除
	$order_info=is_order_exists($order_id);
	if(!$order_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_order_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($warehousing_id,'warehousing','edit'))
	{
		//修改关联订单
		$sql="update ".$ecs->table('erp_warehousing')." set order_id ='".$order_id."' where warehousing_id='".$warehousing_id."'";
		$db->query($sql);
		
		//删除原来的入库单项
		$sql="delete from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."'";
		$db->query($sql);
		
		//新插入入库单项
		if(create_warehousing_item_by_order($order_id,$warehousing_id))
		{
			$result['error']=0;
			die($json->encode($result));				
		}		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}

elseif ($_REQUEST['act'] == 'change_received_qty')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	//检查参数
	if(empty($_REQUEST['warehousing_item_id']) || empty($_REQUEST['warehousing_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$warehousing_item_id=intval($_REQUEST['warehousing_item_id']);
	$received_qty=intval($_REQUEST['received_qty']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库单项是否存在，是否已被删除
	$warehousing_item_info=is_warehousing_item_exists($warehousing_item_id);
	if(!$warehousing_item_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehousing_item_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($warehousing_id,'warehousing','edit'))
	{
		//更改实收数
		$sql="update ".$ecs->table('erp_warehousing_item')." set received_qty='".$received_qty."',account_payable=price*".$received_qty." where warehousing_item_id='".$warehousing_item_id."'";
		$db->query($sql);
		$result['error']=0;
		die($json->encode($result));
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
	
}

elseif ($_REQUEST['act'] == 'delete_warehousing_item')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$warehousing_item_id=intval($_REQUEST['warehousing_item_id']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库单项是否存在，是否已被删除
	$warehousing_item_info=is_warehousing_item_exists($warehousing_item_id);
	if(!$warehousing_item_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehousing_item_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($warehousing_id,'warehousing','edit'))
	{
		$sql="delete from ".$ecs->table('erp_warehousing_item')." where warehousing_item_id='".$warehousing_item_id."'";
		$db->query($sql);
		$result['error']=0;
		die($json->encode($result));
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}	
}

elseif ($_REQUEST['act'] == 'delete_warehousing')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	//检查参数
	if(empty($_REQUEST['warehousing_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	
	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));
	}
	
	if(lock_table($warehousing_id,'warehousing','delete'))
	{
		//删除所有该入库单下的入库单项
		$sql="delete from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."'";
		$db->query($sql);
			
		$sql="delete from ".$ecs->table('erp_warehousing')." where warehousing_id='".$warehousing_id."'";
		$db->query($sql);
		$result['error']=0;
		die($json->encode($result));
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}	
}

elseif ($_REQUEST['act'] == 'check_goods_sn')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	$goods_sn=trim($_REQUEST['goods_sn']);
		
	if(!check_goods_sn($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_warehousing_wrong_goods_sn'];
		die($json->encode($result));		
	}
	else{
		$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
		$goods_id=$GLOBALS['db']->getOne($sql);
		
		$goods_attr=get_goods_attr($goods_id);

		$smarty->assign('goods_attr', $goods_attr);	
		$attr_info=$smarty->fetch('erp_select_goods_attr.htm');

		$result['attr_info']=$attr_info;
		$result['attr_num']=count($goods_attr);		
		$result['error']=0;
		
		die($json->encode($result));			
	}
}


elseif ($_REQUEST['act'] == 'update_warehousing')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$warehousing_id=isset($_REQUEST['warehousing_id'])? intval($_REQUEST['warehousing_id']):0;	
	$goods_sn=isset($_REQUEST['goods_sn'])? trim($_REQUEST['goods_sn']):'';
	$ids=isset($_REQUEST['ids'])? trim($_REQUEST['ids']):'';
	
	if(empty($_REQUEST['goods_qty']))
	{
		$goods_qty=1;
	}
	else{
		$goods_qty=intval($_REQUEST['goods_qty']);
	}
	
	//检查参数
	if(empty($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_warehousing_goods_sn_required'];
		die($json->encode($result));				
	}
	
	//判断产品是否存在
	if(!check_goods_sn($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_warehousing_wrong_goods_sn'];
		die($json->encode($result));		
	}		

	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));
	}
	
	//获取产品Id
	$sql="select goods_id from ".$ecs->table('goods')." where goods_sn='".$goods_sn."'";
	$goods_id=$db->getOne($sql);
	
	if(empty($ids))
	{
			//判断入仓单是否已存在，如已存在，更新数量
			$sql="select warehousing_item_id from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."' and goods_id='".$goods_id."'";
			$warehousing_item_id=$db->getOne($sql);
			
			if(empty($warehousing_item_id))
			{
					$sql="insert into ".$ecs->table('erp_warehousing_item')." set warehousing_id='".$warehousing_id."',goods_id='".$goods_id."', expected_qty='0',received_qty='".$goods_qty."'";			
					$db->query($sql);
					$result['error']=0;
					die($json->encode($result));			

			}
			else{
				$sql="update ".$ecs->table('erp_warehousing_item')." set received_qty=received_qty+'".$goods_qty."' where warehousing_item_id='".$warehousing_item_id."'";
				$db->query($sql);
				$result['error']=0;
				die($json->encode($result));		
			}			
	}
	else{
		//去除最后一个逗号
		$ids=substr($ids,0,strlen($ids)-1);	
		//获取产品属性Id
		$attr_id=get_attr_id($goods_id,$ids);	
		
		//判断入仓单是否已存在，如已存在，更新数量
		$sql="select warehousing_item_id from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."' and goods_id='".$goods_id."' and attr_id='".$attr_id."'";
		$warehousing_item_id=$db->getOne($sql);
			
		if(empty($warehousing_item_id))
		{
					$sql="insert into ".$ecs->table('erp_warehousing_item')." set warehousing_id='".$warehousing_id."',goods_id='".$goods_id."',attr_id='".$attr_id."', expected_qty='0',received_qty='".$goods_qty."'";			
					$db->query($sql);
					$result['error']=0;
					die($json->encode($result));			

		}
		else{
				$sql="update ".$ecs->table('erp_warehousing_item')." set received_qty=received_qty+'".$goods_qty."' where warehousing_item_id='".$warehousing_item_id."'";
				$db->query($sql);
				$result['error']=0;
				die($json->encode($result));		
		}						
	}
}

elseif ($_REQUEST['act'] == 'post_warehousing')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$warehousing_id=intval($_REQUEST['warehousing_id']);

	//判断入仓单是否存在，是否已被删除
	$warehousing_info=is_warehousing_exists($warehousing_id);
	if(!$warehousing_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehousing_not_exist'];
		die($json->encode($result));
	}
	//检查入库单是否完善
	$error=check_warehousing_integrity($warehousing_id);
	if($error==1)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehousing_without_choosing_order'];
		die($json->encode($result));
		}
	elseif($error==2)
	{
		$result['error']=5;
		$result['message']=$_LANG['erp_warehousing_without_warehousing_from'];
		die($json->encode($result));			
	}
	elseif($error==3)
	{
		$result['error']=6;
		$result['message']=$_LANG['erp_warehousing_without_warehousing_item'];
		die($json->encode($result));			
	}
	elseif($error==4)
	{
		$result['error']=7;
		$result['message']=$_LANG['erp_warehousing_without_warehousing_qty'];
		die($json->encode($result));			
	}		
	if(lock_table($warehousing_id,'warehousing','post_to_approve'))
	{
		$sql="update ".$ecs->table('erp_warehousing')." set status='2',post_by='".erp_get_admin_id()."',post_time='".time()."' where warehousing_id='".$warehousing_id."'";
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'add_warehousing')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=add_warehousing();
	
	if($warehousing_id==-1)
	{
		$result['error']=1;
		$result['message']=$GLOBALS['_LANG']['erp_warehousing_without_valid_warehouse'];
		die($json->encode($result));
	}
	else{
		$result['error']=0;
		$result['warehousing_id']=$warehousing_id;
		die($json->encode($result));	
	}
}

elseif ($_REQUEST['act'] == 'withdrawal_to_edit')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	
	$sql="update ".$ecs->table('erp_warehousing')." set status='1' where warehousing_id='".$warehousing_id."'";
		
	$db->query($sql);		
	
	$result['error']=0;
	die($json->encode($result));	
}

elseif ($_REQUEST['act'] == 'approve_pass')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$approve_remark=trim($_REQUEST['approve_remark']);

	if(lock_table($warehousing_id,'warehousing','approve'))
	{
		$warehousing_info=is_warehousing_exists($warehousing_id);
		
		$warehousing_style_id=$warehousing_info['warehousing_style_id'];
		$warehouse_id=$warehousing_info['warehouse_id'];
		$order_id=$warehousing_info['order_id'];
		$warehousing_sn=$warehousing_info['warehousing_sn'];
		$warehousing_from=$warehousing_info['warehousing_from'];
	
		if(!empty($order_id))
		{
			$sql="select order_sn from ".$ecs->table('erp_order')." where order_id='".$order_id."'";
			$order_sn=$db->getOne($sql);				
		}
		else{
			$order_sn='';
		}
		
		$warehousing_item_info=get_warehousing_item_info($warehousing_id);
		
		//如果是关联订单入库，修改相应订单的已入库数
		if($warehousing_style_id==1)
		{
			foreach($warehousing_item_info as $warehousing_item)
			{
					$order_item_id=$warehousing_item['order_item_id'];
					$received_qty=$warehousing_item['received_qty'];
					
					$sql="update ".$ecs->table('erp_order_item')." set warehousing_qty=warehousing_qty+".$received_qty." where order_item_id='".$order_item_id."'";
					$db->query($sql);
			}
		}
		
		//更新出入库序时薄
		foreach($warehousing_item_info as $warehousing_item)
		{
			$goods_id=$warehousing_item['goods_id'];
			$attr_id=$warehousing_item['attr_id'];
			$qty=$warehousing_item['received_qty'];
			$price=$warehousing_item['price'];
			
			$sql="insert into ".$ecs->table('erp_stock')." set goods_id='".$goods_id."',goods_attr_id='".$attr_id."',qty='".$qty."',price='".$price."',amount=qty*price,act_time='".time()."',warehouse_id='".$warehouse_id."'";
			$sql.=", w_d_name='".$warehousing_from."', w_d_style_id='".$warehousing_style_id."',order_sn='".$order_sn."',stock_style='w',w_d_sn='".$warehousing_sn."'";
			
			$db->query($sql);
		}
		
		//如果仓库是成品仓，那么更新网店库存
		if($warehouse_id==1)
		{
			foreach($warehousing_item_info as $warehousing_item)
			{
				$goods_id=$warehousing_item['goods_id'];
				$attr_id=$warehousing_item['attr_id'];
				$qty=$warehousing_item['received_qty'];
			
				//均码
				if(empty($attr_id))
				{
					$sql="update ".$ecs->table('goods')." set goods_number=goods_number+".$qty." where goods_id='".$goods_id."'";
					$db->query($sql);
					
					$sql="update ".$ecs->table('erp_goods_attr')." set goods_qty =goods_qty +".$qty." where goods_id='".$goods_id."' and goods_attr_id_list='$$'";
					$db->query($sql);						
				}
				else{
					$sql="update ".$ecs->table('erp_goods_attr')." set goods_qty=goods_qty+".$qty." where goods_id='".$goods_id."' and attr_id='".$attr_id."'";
					$db->query($sql);								
								
				}
			}
		}
		
		//如果仓库是成品仓,更新网店总库存
		if($warehouse_id==1)
		{
			foreach($warehousing_item_info as $warehousing_item)
			{
				$goods_id=$warehousing_item['goods_id'];
				$sql="select sum(goods_qty) as total_qty from ".$ecs->table('erp_goods_attr')." where goods_id='".$goods_id."'";
				$total_qty=	$db->getOne($sql);	
				$sql="update ".$ecs->table('goods')." set goods_number=goods_number+'".$total_qty."' where goods_id='".$goods_id."'"; //更新库存bug
				$db->query($sql);
			}			
		}		

		//设置入库单状态为4
		$sql="update ".$ecs->table('erp_warehousing')." set status='3',approve_time='".time()."',approved_by='".erp_get_admin_id()."',approve_remark='".$approve_remark."' where warehousing_id='".$warehousing_id."'";
		
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'approve_reject')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	if(empty($_REQUEST['warehousing_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}

	$warehousing_id=intval($_REQUEST['warehousing_id']);
	$approve_remark=trim($_REQUEST['approve_remark']);

	if(lock_table($warehousing_id,'warehousing','approve'))
	{

		$sql="update ".$ecs->table('erp_warehousing')." set status='5',approve_remark='".$approve_remark."' where warehousing_id='".$warehousing_id."'";
		
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_warehousing_no_accessibility'];
		die($json->encode($result));
	}
}



elseif ($_REQUEST['act'] == 'batch_warehousing')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$warehousing_id=intval($_REQUEST['warehousing_id']);

	if(isset($_FILES['file_batch_warehousing']))
	{
		$upload_file_info=$_FILES['file_batch_warehousing'];
		//判断文件类型
		if($upload_file_info['type']!='text/plain')
		{
			$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
			$text=$_LANG['erp_warehousing_return'];
			$link[] = array('href' => $href, 'text' => $text);

   			sys_msg($_LANG['erp_warehousing_only_text_file'], 0, $link);
		}
		//转移并重命名文件
		$souce_file=$upload_file_info['tmp_name'];
		$upload_file=format_file($souce_file);
		
		//检查是否是合法的入库文件
		$warehousing_info=check_file($upload_file);
		
		if(!is_array($warehousing_info))
		{
			if($warehousing_info==1)
			{
				$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
				$text=$_LANG['erp_warehousing_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_warehousing_invalid_file'], 0, $link);					
			}
			elseif($warehousing_info==2)
			{
				$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
				$text=$_LANG['erp_warehousing_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_warehousing_invalid_goods_sn'], 0, $link);					
			}
			elseif($warehousing_info==3)
			{
				$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
				$text=$_LANG['erp_warehousing_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_warehousing_invalid_goods_size'], 0, $link);					
			}
		}
		else{
			foreach($warehousing_info as $warehousing_item)
			{
				$goods_sn=$warehousing_item['goods_info']['goods_sn'];
				$goods_size=$warehousing_item['goods_info']['goods_size'];
				$warehousing_qty=$warehousing_item['qty'];
				
				$sql="select goods_id from ".$ecs->table('goods')." where goods_sn='".$goods_sn."'";
				$goods_id=$db->getOne($sql);
				
				if(lock_table($warehousing_id,'warehousing','edit'))
				{
					$sql="select warehousing_item_id from ".$ecs->table('erp_warehousing_item')." where warehousing_id='".$warehousing_id."' and goods_id='".$goods_id ."' and goods_size='".$goods_size."'";
					$warehousing_item_id=$db->getOne($sql);
					if(!empty($warehousing_item_id))
					{
						$sql="update ".$ecs->table('erp_warehousing_item')." set received_qty=received_qty+".$warehousing_qty." where warehousing_item_id='".$warehousing_item_id."'";
						$db->query($sql);
					}
					else{
						$sql="insert into ".$ecs->table('erp_warehousing_item')." set warehousing_id=".$warehousing_id.",goods_id='".$goods_id."',goods_size='".$goods_size."',received_qty=".$warehousing_qty;
						$db->query($sql);
					}
				}
				else{
					$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
					$text=$_LANG['erp_warehousing_return'];
					$link[] = array('href' => $href, 'text' => $text);

   					sys_msg($_LANG['erp_warehousing_no_accessibility'], 0, $link);	
				}				
			}
		}			
	}
		$href="erp_warehouse_manage.php?act=edit_warehousing&id=".$warehousing_id;
		$text=$_LANG['erp_warehousing_return'];
		$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_warehousing_batch_warehousing_success'], 0, $link);		
}

?>